/*
 * @Author: scopesang
 * @Date: 2022-05-10 13:24:42
 * @LastEditors: scopesang
 * @LastEditTime: 2022-05-10 13:28:33
 * @FilePath: \TypeScript\src\class\other.ts
 * @Description:
 *
 * Copyright (c) 2022 by scopesang/大鹏科技, All Rights Reserved.
 */
// 类属性和方法除了可以通过 extends 被继承之外，还可以通过修饰符控制可访问性。
// public、private、protected
class Animal {
    public name: string;
    private age: number;	// 只能在当前类中使用，实例和子类都不能用
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    public sayName(): void {
        console.log(`${this.name}今年${this.age}岁`);
    }
}

let animal = new Animal("张三", 11);
animal.sayName();           // 张三今年11岁
console.log(animal.age);    // 属性“age”为私有属性，只能在类“Animal”中访问 ✖


class Animal {
    public name: string;
    protected age: number;  // 只能在当前类和子类中使用，实例不能用
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

let animal = new Animal("张三", 11);
console.log(animal.age);    // 属性“age”受保护，只能在类“Animal”及其子类中访问 ✖

class Cat extends Animal {
    constructor(name: string, age: number) {
        super(name, age);
    }
    shout(): void {
        console.log(`${this.name}正在喊“喵喵”，它今年${this.age}岁`);	 // 可以使用 ✔
    }
}

let cat = new Cat("嘟嘟", 11);
cat.shout();   // 嘟嘟

